﻿using System;
using System.Collections.Generic;
using wojilu.Apps.Qycms.Domain;
using wojilu.Apps.Qycms.Service;
using wojilu.Members.Users.Domain;
using wojilu.Net;
using wojilu.Web.Controller;

namespace wojilu.Apps.Qycms.Helper
{
    public class YBBMailSend
    {
        public bool MailSend(string mailcode, string mail, string Lngs, Dictionary<string, object> dict)
        {
            var model = Templates.find(" lng='" + Lngs + "' and templatecode='" + mailcode + "' ").first();
            if (model != null)
            {
                var templatecontent = model.templatecontent;
                var title = model.title;
                var fs = new FastReplacer("[%", "%]");
                var fs1 = new FastReplacer("[%", "%]");
                fs.Append(templatecontent);
                fs1.Append(title);
                var dtrow = cdb.findBy<MailTemplate>("ClassType", model.typeclass);
                switch (model.typeclass)
                {
                    case "mailbbs":
                        var modelBbs = Bbs.findById(dict["Id"].ObjectToInt());
                        if (modelBbs == null) return false;
                        foreach (var row in dtrow)
                        {
                            switch (row.Title)
                            {
                                case "[%forumlink%]":
                                    fs.Replace(row.Title, UrlLinkHelp.GetForumLink(dict["Id"], YBBCommon.GetConfigStr("home_lng"), Lngs));
                                    break;

                                default:
                                    if (!modelBbs.get(row.Content).IsNull())
                                    {
                                        fs.Replace(row.Title, modelBbs.get(row.Content).ToString());
                                    }
                                    else
                                    {
                                        fs.Replace(row.Title, string.Empty);
                                    }
                                    break;
                            }
                        }
                        break;

                    case "mailshangjoin":
                        foreach (var row in dtrow)
                        {
                            fs.Replace(row.Title, dict[row.Content.ToString()].ToString());
                        }
                        break;

                    case "mailenquiry":
                        var modelEnquiry = Enquiry.findById(dict["eid"].ObjectToInt());
                        if (modelEnquiry == null) return false;
                        foreach (var row in dtrow)
                        {
                            switch (row.Title)
                            {
                                case "[%entime%]":
                                    fs.Replace(row.Title, modelEnquiry.get("addtime").ToString());
                                    break;

                                default:
                                    if (!modelEnquiry.get(row.Content).IsNull())
                                    {
                                        fs.Replace(row.Title, modelEnquiry.get(row.Content).ToString());
                                        fs1.Replace(row.Title, modelEnquiry.get(row.Content).ToString());
                                    }
                                    else
                                    {
                                        fs.Replace(row.Title, string.Empty);
                                        fs1.Replace(row.Title, string.Empty);
                                    }
                                    break;
                            }
                        }
                        break;

                    case "mailmember":
                        var modelMember = User.findById(TypeCon.ObjectToInt(dict["userid"]));
                        if (modelMember == null) return false;
                        foreach (var row in dtrow)
                        {
                            switch (row.Title)
                            {
                                case "[%checklink%]":
                                    fs.Replace(row.Title, UrlLinkHelp.MemberEmailMarketing(Lngs, modelMember, dict["userid"]));
                                    break;

                                case "[%m_username%]":
                                    fs.Replace(row.Title, modelMember.Name);
                                    fs1.Replace(row.Title, modelMember.Name);
                                    break;

                                case "[%m_email%]":
                                    fs.Replace(row.Title, modelMember.Email);
                                    break;

                                case "[%question%]":
                                    fs.Replace(row.Title, modelMember.Question);
                                    break;

                                case "[%answer%]":
                                    fs.Replace(row.Title, modelMember.Answer);
                                    break;

                                case "[%m_time%]":
                                    fs.Replace(row.Title, modelMember.Created.ToString());
                                    break;

                                case "[%rankname%]":
                                    var mc = Member_Class.findById(modelMember.mcid);
                                    fs.Replace(row.Title, mc != null ? mc.rankname : String.Empty);
                                    break;

                                case "[%newpassword%]":
                                    try
                                    {
                                        fs.Replace(row.Title, dict["newpassword"].ToString());
                                    }
                                    catch
                                    {
                                    }
                                    break;
                            }
                        }
                        break;

                    case "mailform":
                        var modelformvalue = Form_Value.findById(TypeCon.ObjectToInt(dict["Id"]));
                        if (modelformvalue == null) return false;
                        var modelformgroup = Form_Group.findById(dict["fgid"].ObjectToInt());
                        foreach (var row in dtrow)
                        {
                            switch (row.Title)
                            {
                                case "[%formgroupname%]":
                                    fs.Replace(row.Title, modelformgroup.formgroupname);
                                    fs1.Replace(row.Title, modelformgroup.formgroupname);
                                    break;

                                case "[%attrvalue%]":
                                    var builder = new MyStringBuilder();
                                    var list = Form_Attr.find(" fgid =:f").set("f", modelformgroup.Id).list();
                                    builder.Append("<table width=\"100%\" border=\"0\">");
                                    foreach (var row1 in list)
                                    {
                                        if (row1.isline != 1)
                                        {
                                            builder.AppendLine("					<tr class=\"trstyle2\">");
                                            builder.AppendLine("						<td class=\"trtitle01\">" + row1.typename + "</td>");
                                            builder.AppendLine("						<td class=\"trtitle02\">");
                                            builder.AppendLine("							" + modelformvalue.get(row1.attrvalue));
                                            builder.AppendLine("						</td>");
                                            builder.AppendLine("					</tr>");
                                        }
                                    }
                                    builder.Append("</table>");
                                    fs.Replace(row.Title, builder.ToString());
                                    break;

                                case "[%recontent%]":
                                    fs.Replace(row.Title, modelformvalue.get("recontent").ToString());
                                    break;

                                case "[%f_time%]":
                                    fs.Replace(row.Title, modelformvalue.get("addtime").ToString());
                                    break;
                            }
                        }
                        break;
                }
                fs.Replace("[%nowtime%]", DateTime.Now.ToString());
                fs.Replace("[%sitename%]", new ConfigsService().GetValue("sitename"));
                MailClient ybbmail = MailClient.Init();
                Result sentResult = ybbmail.Send(mail, fs1.ToString(), fs.ToString());
                if (sentResult.IsValid)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }
    }
}